import java.util.HashMap;
import java.util.TreeSet;

/**
* @description: leetCode 周赛303 t3
* @author hewei
* @date 2022/7/24 10:44
* @version 1.0
*/

public class FoodRatings {

    HashMap<String, TreeSet<Integer>> cuisinesPriority;
    String[] foods;
    String[] cuisines;
    int[] ratings;
    HashMap<String, Integer> foodIndex;
    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        this.cuisines = cuisines;
        this.foods = foods;
        this.ratings = ratings;
        foodIndex = new HashMap<>();
        cuisinesPriority = new HashMap<>();
        for (int i = 0; i < cuisines.length; i++) {
            String cuisine = cuisines[i];
            if (cuisinesPriority.containsKey(cuisine)) {
                cuisinesPriority.get(cuisine).add(i);
            } else {
                TreeSet<Integer> temp = new TreeSet<>((a, b) -> {
                    if (ratings[a] != ratings[b]) return ratings[b] - ratings[a];
                    return foods[a].compareTo(foods[b]);
                });
                temp.add(i);
                cuisinesPriority.put(cuisine, temp);
            }
            foodIndex.put(foods[i], i);
        }
    }

    public void changeRating(String food, int newRating) {
        int index = foodIndex.get(food);
        String cui = cuisines[index];
        TreeSet<Integer> heap = cuisinesPriority.get(cui);
        heap.remove(index);
        ratings[index] = newRating;
        heap.add(index);
    }

    public String highestRated(String cuisine) {
        return foods[cuisinesPriority.get(cuisine).first()];
    }
}
